/*
board-games:	An educational framework for board games programming.
    Copyright (C) 2008  Daniel Gutson, FuDePAN
    
    This file is part of board-games.

    board-games is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    board-games is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
*/
#include "connect4_proxy.h"
#include "player_sdk/player.h"

size_t random_seed(0);


class Connect4Player : public Player
{
    Board<PLAYERS, BOARD_ROWS, BOARD_COLS> board;
    PLAYERS team;
    PlayerAPI* const api;
    
    PLAYERS other() const
    {
        return static_cast<PLAYERS>(3 - team);
    }
    
    void receive_team()
    {
        api->receive_message_from_referee(team);
        init_player(team);
    }
    
    bool receive_others_move()
    {
        bool keep_playing;
        
        //first message is keep_playing:        
        api->receive_message_from_referee(keep_playing);
        
        return keep_playing;
    }

    virtual void play()
    {
        Coord played_col;
        bool keep_playing = true;
        
        receive_team();
        
        api->connect_board_with_referee(board);
        
        
        if (team == BOARD_O)            //we are second
            keep_playing = receive_others_move();
        
        while(keep_playing)
        {
            ::play(board.get_board_memory(), &played_col);
            //update our board
            //board[move.row][move.col] = team;
            api->send_message_to_referee(played_col);
            
            keep_playing = receive_others_move();
        }
    }
    
public:
    Connect4Player(PlayerAPI* api)
        : api(api), board(BOARD_EMPTY)
    {
                
        team = BOARD_EMPTY;
        random_seed = api->get_srand();
    }
    
};

Player* create_player(PlayerAPI* api)
{
    return new Connect4Player(api);
}


extern "C" {
unsigned int get_random_seed(void)
{
    return random_seed;
}
}

